/****************************************************************
 * <strong>MFDataBar工具条</strong>
 * <p>
 * 工具条
 * </p>
 *
 * @author AndyZhang 2011
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.vdata;

import com.thinkwide.data.exchange.MFXml;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MFDataBar {
    private List<MFDataButton> buttons;

    public MFDataBar() {
        buttons = new ArrayList<MFDataButton>();
    }

    public void setButtons(List<MFDataButton> buttons) {
        this.buttons = buttons;
    }

    public List<MFDataButton> getButtons() {
        return buttons;
    }

    public boolean loadFromXml(String xml) {
        Document doc = MFXml.string2Document(xml);
        Element root = doc.getRootElement();

        for (Iterator<?> i = root.elementIterator(); i.hasNext(); ) {
            MFDataButton item = new MFDataButton();
            Element elem = (Element) i.next();
            item.setName(MFXml.getAttrValue(elem, "name"));
            item.setText(MFXml.getAttrValue(elem, "text"));
            item.setIcon(MFXml.getAttrValue(elem, "icon"));
            item.setOnClick(MFXml.getAttrValue(elem, "onClick"));
            item.setVisible(MFXml.getAttrValue(elem, "visible"));
            item.setEnable(MFXml.getAttrValue(elem, "enable"));

            buttons.add(item);
        }

        return true;
    }

    public String SaveToXml() {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("bar");
        for (MFDataButton item : buttons) {
            Element node = root.addElement("item");
            node.addAttribute("name", item.getName());
            node.addAttribute("text", item.getText());
            node.addAttribute("icon", item.getIcon());
            node.addAttribute("onClick", item.getOnClick());
            node.addAttribute("visible", item.getVisible());
            node.addAttribute("enable", item.getEnable());

        }

        return doc.asXML();
    }

}

